"""
Shows real-time system statistics.
"""

import os
import time
import psutil
from sys_info_poll import poll
from bytestohuman import bytes2human
import copy

BFH = r'%'


def refresh_window(cpu_state, memory_state, system_time, user_info,
                   disk_info, processinfo,netstat,nic_info
                   ):
    if os.name == 'nt':
        os.system("cls")
    else:
        os.system("clear")
    """Print stats on screen."""
    # print boot time and uptime----------------------------------------------------------------------------------------
    print(" %10s Boot Time：%10s Uptime: %10s " % (system_time[0], system_time[1], system_time[2]))
    # Print Login information-------------------------------------------------------------------------------------------
    for i in range(len(user_info[0])):
        print(" %-15s:%s host: %s Login in Time: %s" % ("User",
                                                        user_info[0][i],
                                                        user_info[1][i],
                                                        user_info[2][i]))
    # print current time #cpu state #memory-----------------------------------------------------------------------------
    print(" %-15s:%s %s%s" % ("CPU", cpu_state[0], cpu_state[1], BFH))
    print(" %-15s:%s%% %6s/%s" % ("Memory",
                                  memory_state[0],
                                  str(memory_state[1]) + "M",
                                  str(memory_state[2]) + "M"
                                  ))
    for i in disk_info:
        print(" %-15s:%s Volume: %5s GB Free: %5s GB Used: %5s%s FS Type: %5s"
              % ("Mount Point", i[0], i[1], i[2], i[3], BFH, i[4]))
    # Process information-----------------------------------------------------------------------------------------------
    """
    processinfo[0][0]: Process name
    processinfo[0][1]: Process PID
    processinfo[0][2]: Process status
    processinfo[0][3]: Process Mem percent
    processinfo[0][4]: Process Mem value
    processinfo[0][5]: Process Creation time
    processinfo[0][6]: Process Cpu usage rate
    processinfo[1]:    The total Process numbers
    """
    try:
        print(" %-15s:%d" % ("Process Numbers", processinfo[1]))
        print(" --------------------------------------------------------------------------------")
        print(" %-30s %10s %-10s %9s %17s" % ("Image Name", "PID",
                                            "STATUS",
                                            "CPU %",
                                            "MEM"))
        print(" --------------------------------------------------------------------------------")

        for i in processinfo[0]:
            print(" %-30s %10s %-10s %8.1f%s %17s"
                  % (i[0], i[1], i[2], i[6], BFH, (str("%5.2f" % i[3]) + "%|" + str(bytes2human(i[4])))
                     )
                  )

        print("--------------------------------------------------------------------------------")
    except:
        pass
    # Network Stats-----------------------------------------------------------------------------------------------------
    print(" %-15s:sent: %-10s received: %s" % ("total bytes", netstat[0],
                                                   netstat[1])
              )
    print(" %-15s:sent: %-10s received: %s" % ("total packets", netstat[2],
                                                   netstat[3])
              )
    print(" %-30s %-15s %-10s %-10s %-10s %-10s" % ("NAME",
                                                          "IP Address",
                                                          "Sent(S)",
                                                          "Recive(S)",
                                                          "Sent(P)",
                                                          "Recive(P)",
                                                          ))
    for i in nic_info:
        print(" %-30s %-15s %-10s %-10s %-10s %-10s" % (i[0],
                                                        i[1],
                                                        str(bytes2human(i[3])),
                                                        str(bytes2human(i[4])),
                                                        str(i[5]),
                                                        str(i[6]),
                                                        ))
    # ------------------------------------------------------------------------------------------------------------------

def show_sys_info():
    try:
        display_line_number = 15  # 用来设定显示process的数量
        while True:
            args = poll(display_line_number)
            refresh_window(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7])
    except (KeyboardInterrupt, SystemExit):
        pass

if __name__ == "__main__":
    show_sys_info()

